# -*- coding: UTF-8 -*-
# @Project ：VehicleDimensionMeasurementSystem 
# @File    ：measure_mian_window.py
# @IDE     ：PyCharm 
# @Date    ：2024/6/11 下午10:07
import sys

from PySide6.QtCore import QTimer
from loguru import logger
from PySide6.QtWidgets import (QWidget, QApplication, QMessageBox)

from resources.ui.vehicle_mianUi import Ui_Form
from source.modules.ui_function import UiFunctions
from source.modules.app_function import AppFunction

logger.add("../log/vehicle.log", rotation="5MB", encoding="utf-8", enqueue=True, retention="10 days")


class MeasureMainWindow(QWidget, Ui_Form):
    def __init__(self):
        super().__init__()
        self.overall_calculated_result_dict = None  # 外廓计算后的结果
        self.weigh_calculated_result_dict = None  # 整备质量计算后的结果
        self.setupUi(self)
        self.current_timer = QTimer()  # 展示当前时间
        UiFunctions.init_ui(self)
        self.wait_response_thread = None  # 等待外廓测量的线程
        self.wait_weigh_thread = None  # 等待称重的线程
        self.vehicle_info_object = None  # 车辆信息
        self.inspection_item_dict = None  # 检验项目
        self.measure_record_id = None  # 检验记录的ID
        AppFunction.get_inspection_item(self)
        self.bind()

    def bind(self):
        """信号与槽的绑定"""
        self.btnStart.clicked.connect(self.start_measure)  # 开始检测
        self.btnPrintReport.clicked.connect(self.print_report)  # 打印

    def start_measure(self):
        """开始检测的逻辑"""
        # 1. 单机检测模式
        AppFunction.alon_networking_mode(mode='stand_alone_mode', widgets=self)

    def overall_thread_state(self, result_dict: dict):
        """线程执行过程中的状态，会返回一个字典"""
        try:
            self.overall_calculated_result_dict = AppFunction.process_overall_result(self, result_dict,
                                                                                     self.inspection_item_dict,
                                                                                     self.vehicle_info_object)
        except Exception as e:
            QMessageBox.warning(self, '系统提示', '未获取到正确的配置文件')

    def weigh_thread_state(self, weight_value_dict):
        """线程执行过程中的状态，返回的是字典，实时展示在LCD中"""
        self.weigh_calculated_result_dict = AppFunction.process_weight_result(self, weight_value_dict,
                                                                              self.vehicle_info_object)

    def print_report(self):
        """实现打印报告单"""
        AppFunction.print_report(self.measure_record_id)


def main():
    app = QApplication(sys.argv)
    window = MeasureMainWindow()
    window.show()
    app.exec()


if __name__ == '__main__':
    main()
